using UnityEngine;
using System;
using System.Collections;

public class DayNightCycle : MonoBehaviour {
	
	public Transform Sun;
	public bool Activate = true;
	public Camera MainCamera;
	public float dayCycleInMinutes = 1;
	public float startHour = 0;
	public GUIText horloge;
	public GameObject ExteriorLights;
	
	public Color NightSkyColor, DaySkyColor;
	private float alpha = 0;
	private float deltaTime = 0.000f;
	private float tempTime = 0.000f;
	
	private const float SECOND = 1;
	private const float MINUTE = 60 * SECOND;
	private const float HOUR = 60 * MINUTE;
	private const float DAY = 24 * HOUR;
	
	private const float DERGREES_PER_SECOND = 360/DAY;
	
	private float timeOfDay;
	private float degreeRotation;
	private bool jour = false;
	
	// Use this for initialization
	void Start () {
		//calcul de combien de degrée le soleil tournera en 1 sec
		degreeRotation = 360 / ( dayCycleInMinutes * MINUTE);
		timeOfDay = ((startHour*HOUR)*(dayCycleInMinutes*MINUTE)/DAY);
		Sun.Rotate(new Vector3(timeOfDay*degreeRotation, 0, 0));
		tempTime = float.Parse(Network.time.ToString());
	}
	
	void OnConnectedToServer(){
		networkView.RPC("SendTimeOfDay", RPCMode.Server);
	}
	
	[RPC]
	IEnumerator SendTimeOfDay(){
		yield return new WaitForSeconds(5);
		networkView.RPC("ApplyTimeOfDay", RPCMode.Others, timeOfDay);
	}
	
	[RPC]
	void ApplyTimeOfDay(float serverTimeOfDay){
		timeOfDay = serverTimeOfDay;
		Sun.Rotate(new Vector3(timeOfDay*degreeRotation, 0, 0));
	}
	
	// Update is called once per frame
	void Update () {
		if(Network.isServer){
			deltaTime = float.Parse(Network.time.ToString())-tempTime;
			tempTime = float.Parse(Network.time.ToString());
		}
		if(Network.isClient){
			networkView.RPC("SendTimeOfDay", RPCMode.Server);
		}
		if(Activate){
			//effectue une rotation du soleil en fonction du temps écoulé
			var angle = (timeOfDay*degreeRotation)-90;
			Sun.localEulerAngles = new Vector3(angle,0,0);
			//modifie la couleur du ciel en fonction de l'heure
			MainCamera.backgroundColor = Color.Lerp(NightSkyColor, DaySkyColor, alpha);
			//incrémente l'heure
			timeOfDay += deltaTime;
			//Debug.Log("timeofday:"+timeOfDay);
			//Debug.Log("degree:"+degreeRotation);
			//Debug.Log("angle:"+angle);
			if(((timeOfDay < (dayCycleInMinutes*MINUTE/4))||(timeOfDay > (3*dayCycleInMinutes*MINUTE/4)))){
				jour = true;
				ExteriorLights.SetActiveRecursively(true);
				if(alpha > 0){
					alpha -= deltaTime;
					Sun.light.intensity -= deltaTime;
				}
				else{
					alpha = 0;
					Sun.light.intensity = 0;
				}
			}
			
			
			else {
				jour = false;
				ExteriorLights.SetActiveRecursively(false);
				if(alpha < 1){
					alpha += deltaTime;
					Sun.light.intensity += deltaTime;
				}
				else{
					alpha = 1;
					Sun.light.intensity = 0.8f;
				}
			}
			
			
			if(timeOfDay >= dayCycleInMinutes*MINUTE){
				timeOfDay -= dayCycleInMinutes*MINUTE;
			}
		}
		
		
		Int32 heure,minute,seconde;
		heure = (int)(((timeOfDay*(DAY/(dayCycleInMinutes*MINUTE)))/3600))%24;
		minute = (int)((timeOfDay*(DAY/(dayCycleInMinutes*MINUTE)))/60)%60;
		seconde = (int)(timeOfDay*(DAY/(dayCycleInMinutes*MINUTE)))%60;
		horloge.text = heure.ToString("00")+":"+minute.ToString("00");
	}
}
